<?php

namespace App\Admin\Controllers;

use App\Topic;
use Illuminate\Http\Request;

class TopicController extends Controller
{
    //
    public function index()
    {
        $topics = Topic::orderBy('created_at', 'desc')->paginate(10);
        return view('admin.topic.index', compact('topics'));
    }

    //添加专题
    public function create()
    {
        return view('admin.topic.add');
    }

    public function store()
    {
//        //验证
        $this->validate(\request(), [
            'name' => 'required | string',
        ]);

        $params = \request(['name']);
         $topic = Topic::create($params);

        //渲染
        return redirect('admin/topics');
    }

    //删除
    public function destroy(Topic $topic)
    {
        $topic->delete();

        return [
            'code' => 200,
            'msg' => '删除成功',
        ];
    }

}
